#include<stdio.h>
#include<stdlib.h>

int *a=NULL,ctr=1,num,ret=0;

void append(int x)
{   if(ctr==1)
       a=malloc(1*sizeof(int));
    else
       a=realloc(a,(ctr*sizeof(int)));
    if(a==NULL)
       printf("\n\tMemory allocation error");
    else
    {   a[ctr-1]=x;
        ctr++;
        printf("\n\tElement appended\n");
    }
}//append

void extend(int b[])
{   int i;
    if(ctr==1)
    {   a=malloc(num*sizeof(int));
        if(a==NULL)
           printf("\n\tMemory allocation error");
        for(i=0;i<num;i++)       
        {   a[ctr-1]=b[i];
            ctr++;
        }
    }//if
    else
    {   for(i=0;i<num;i++)
    	{   a=realloc(a,(ctr*sizeof(int)));
            if(a==NULL)
               printf("\n\tMemory allocation error");
            a[ctr-1]=b[i];
            ctr++;
        }//for
    }//else       
    printf("\n\tList Extended\n");
}//extend

void insert(int x,int pos)
{   int i,flag=0,temp;
    if(ctr==1)
    {   if(pos!=0)
        {   printf("\n\tInvalid position");
        }//if
        else
        {   a=malloc(1*sizeof(int));
            a[ctr-1]=x;
            ctr++;
        }//else
    }   
    else
    {   a=realloc(a,((ctr)*sizeof(int)));
        if(a==NULL)
           printf("\n\tMemory allocation error");
    	if(pos<ctr  && pos>-1)
    	{   temp=a[pos];
            a[pos]=x;
            ctr++;
            flag=1;
            for(i=pos+1;i<ctr;i++)
            {   x=temp;
                temp=a[i];
                a[i]=x;
            }       
            printf("\n\tElement inserted");
            flag=1;
        }
        if(flag==0)
           printf("\n\tInvalid position");
    }//else
}//insert

void remve(int x)
{   int i,j,flag=0;
    for(i=0;i<ctr;i++)
       if(a[i]==x)
         flag=1;
    if(flag==1)
    {   for(j=i;j<ctr;j++)
           a[i]=a[i+1];
        ctr-=1;
    }
    if(flag==0)
      printf("\n\tValue not found");
    else
    {   printf("\n\tElement removed");
        a=realloc(a,((ctr)*sizeof(int)));
    }
}//remove

int pop(int pos)
{   int flag=0,pop,i;
    if(pos==-1)
    {   flag=1;
        pop=a[ctr-1];
        ctr--;
    }//if
    else if(pos>-1 && pos<ctr-1)
    {   pop=a[pos];
        for(i=pos;i<(ctr-1);i++)
        {   a[i]=a[i+1];
            flag=1;   
        }
        ctr-=1;
    }//else
    if(flag==1)
    {   printf("\n\tElement removed");
        a=realloc(a,((ctr)*sizeof(int)));
        return pop;
        ret=0;
    }
    else 
    {   printf("\n\tInvalid position");
        ret=1;
        return ret;
    }
}//pop

int indx(int x)
{   int i,flag=0,pos;
    for(i=0;i<ctr;i++)
    {   if(a[i]==x)
        {   flag=1;
            pos=i;
            break;
        }
    }//for
    if(flag==0)
    {   printf("\n\tValue not found");
        ret=1;
        return ret;
    }
    else
    {   return pos;
        ret=0;
    }//else
}//index

int count(int x)
{   int num=0,i,flag=0;
    for(i=0;i<ctr;i++)
       if(a[i]==x)
       {   flag=1;
           ret=0;
           num++;
       }//if
    if(flag==0)
    {   printf("\n\tValue not found");
        ret=1;
        return ret;
    }
    else
    {   return num;
        ret=0;
    }//else
}//count

void reverse()
{    int temp,j;
     j=ctr-2;
     int i=0;
     while(i<j)
     {  temp=a[i];
        a[i]=a[j];
        a[j]=temp;
        i++;
        j--;       
     }//while
}//reverse

void sort(int x)
{   int i,j,temp;
    for(i=0;i<=ctr-2;i++)
       for(j=i+1;j<ctr-1;j++)
	   if(a[i]>a[j])
	   {   temp=a[i];
	       a[i]=a[j];
	       a[j]=temp;
	   }
        if(x==1)
           reverse();
}//sort		

void display()
{ int i;
  printf("\n\t[");
  for(i=0;i<ctr-1;i++)
  {   if(i!=0)
        printf(", ");   
      printf("%d",a[i]);
  }
  printf("]\n");
}//display

void main()
{   int ch,x;
    do
    {   printf("\n\n\tPython list in C");
        printf("\n\n\t1)Append an element");
        printf("\n\t2)Extend the list");
        printf("\n\t3)Insert anywhere in list");
        printf("\n\t4)Remove an element");
        printf("\n\t5)Pop an element");
        printf("\n\t6)Search 1st position of an element");
        printf("\n\t7)Number of occurences");
        printf("\n\t8)Sort the list"); 
        printf("\n\t9)Reverse the list");
        printf("\n\t10)Exit");
        printf("\n\n\tEnter choice:");
        scanf("%d",&ch);
        switch(ch)
        {   case 1:{   printf("\n\tEnter value to be inserted:");
                       scanf("%d",&x);
                       append(x);
                       display();
                       break;
                   }
            case 2:{  int i;
                      printf("\n\tHow many elements you want to add:");
                      scanf("%d",&num);
                      int a[num];
                      for(i=0;i<num;i++)
                      {   printf("\n\tEnter value:");
                          scanf("%d",&a[i]);
                      }//for
                      extend(a);
                      display();
                      break; 
                   }
            case 3:{   int pos;
                       printf("\n\tEnter value:");
      	               scanf("%d",&x);
      		       printf("\n\tEnter position to be inserted(0 for beginning):");
                       scanf("%d",&pos);
                       insert(x,pos);       
                       display();
                       break;
                   }
            case 4:{   if(ctr==1)
                          printf("\n\tList is empty");
                       else
                       {   printf("\n\tEnter value:");
      	                   scanf("%d",&x);
      		           remve(x);       
                           display();
                       }
                       break;
                   }
            case 5:{   if(ctr==1)
                          printf("\n\tList is empty");
                       else
                       {   int v;
                           printf("\n\tEnter the  position (0 for 1st element,-1 for no entry):");
                           scanf("%d",&x);
                           v=pop(x);
                           if(ret==0)
                              printf("\n\tThe value popped is=%d",v);
                           display();
                       }//else
                       break;
                   }
            case 6:{   if(ctr==1)
                         printf("\n\tList is empty");
                       else
                       {   int v;
                           printf("\n\tEnter value:");
      	                   scanf("%d",&x);
        	           v=indx(x);
                           if(ret==0)       
                             printf("\n\tThe first occurence of element is at position %d",v);
                       }//else
                       break;
                   }
            case 7:{  if(ctr==1)
                         printf("\n\tList is empty");   
                      else
                      {   int v;
                          printf("\n\tEnter value:");
                          scanf("%d",&x);
                          v=count(x);
                          if(ret==0)       
                             printf("\n\tNumber of occurences of %d is %d",x,v);
                      }//else
                      break;
                   }
            case 8:{   if(ctr==1)
                          printf("\n\tList is empty");
                       else
                       {  printf("\n\tEnter 0 for ascending,1 for descending:");
                          scanf("%d",&x);            
                          if(x!=0 || x!=1)
                             printf("\n\tInvalid Input");
                          else
                          {   sort(x);
                              printf("\n\tDisplaying the list in sorted order...\n");
                              display();
                          }//else
                        }//else
                        break;
                   }       
            case 9:{   if(ctr==1)
                          printf("\n\tList is empty");
                       else
                       {   reverse();
                           printf("\n\tDisplaying the list in reverse order...\n");
                       display();
                       }//else
                       break;
                   }
           case 10:{   printf("\nQuitting...\n");
                       exit(1);
                       break;
                   }
           default:printf("\n\tInvalid choice...");
        }//switch
    }while(ch!=10);             
}//main


